<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>
            3D化学后台管理系统
        </title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="format-detection" content="telephone=no">
        <link rel="stylesheet" href="./css/x-admin.css" media="all">
    </head>
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>管理员管理</cite></a>
              <a><cite>管理员列表</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
        </div>
        <div class="x-body">
            <form id="form-query" class="layui-form x-center" action="">
                <div class="layui-form-pane" style="margin-top: 15px;">
                  <div class="layui-form-item">
                    <label class="layui-form-label">查询</label>
                    <div class="layui-input-inline">
                      <input type="text" id="adminName-query" name="adminName"  placeholder="请输入登录名" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-input-inline" style="width:80px">
                        <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
                    </div>
                  </div>
                </div> 
            </form>
            <xblock id="xblock">
                <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon">&#xe640;</i>批量删除</button>
                <button class="layui-btn" onclick="admin_add('添加管理员','admin-add.html','','','550','500')"><i class="layui-icon">&#xe608;</i>添加</button>
                <span id="span-count" class="x-right" style="line-height:40px">共有数据：0 条</span>
            </xblock>            
            <table class="layui-table">
                <thead>
                    <tr>
                        <th id="th-check">
                            <input type="checkbox" name="" value="">
                        </th>
                        <th>
                            ID
                        </th>
                        <th>
                            登录名
                        </th>
                        <th>
                            角色
                        </th>
                        <th>
                            状态
                        </th>
                        <th id="th-manage">
                            操作
                        </th>
                    </tr>
                </thead>
                <tbody id="admin-list">
                    <tr>
                        <td id="td-check">
                            <input type="checkbox" value="1" name="">
                        </td>
                        <td>
                            1
                        </td>
                        <td>
                            admin
                        </td>
                        <td >
                            超级管理员
                        </td>
                        <td id="td-status" class="td-status">
                            <span class="layui-btn layui-btn-normal layui-btn-mini">
                                已启用
                            </span>
                        </td>
                        <td id="td-manage" class="td-manage">
                            <a style="text-decoration:none" onclick="admin_stop(this,'10001')" href="javascript:;" title="停用">
                                <i class="layui-icon">&#xe601;</i>
                            </a>
                            <a title="修改密码" href="javascript:;" onclick="admin_edit(event,'修改密码','admin-edit.html','','','600','500')"
                            class="ml-5" style="text-decoration:none">
                                <i class="layui-icon">&#xe642;</i>
                            </a>
                            <a title="删除" href="javascript:;" onclick="admin_del(event)" 
                            style="text-decoration:none">
                                <i class="layui-icon">&#xe640;</i>
                            </a>
                        </td>
                    </tr>
                </tbody>
            </table>

            <div id="page" class="layui-laypage"></div>

        </div>
        <script src='./js/jquery.min.js' type='text/javascript' ></script>
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script src="./js/x-layui.js" charset="utf-8"></script>
        <script src="http-config.js"></script>

        <script>
             // 获取 URL 中的查询字符串
             var queryString = window.location.search;
            // 创建 URLSearchParams 对象
            var searchParams = new URLSearchParams(queryString);
            // 获取参数值
            var adminRole = searchParams.get('adminRole');
            console.log("admin-list" + adminRole);
            if(adminRole == "普通管理员"){
                $("#xblock").hide();
                $("#th-check").hide();
                $("#td-check").hide();
                $("#th-manage").hide();
                $("#td-manage").hide();
            }

            layui.use(['laydate','element','laypage','layer','form'], function(){
                $ = layui.jquery;//jquery
              laydate = layui.laydate;//日期插件
              lement = layui.element();//面包导航
              laypage = layui.laypage;//分页
              layer = layui.layer;//弹出层
              form = layui.form();

              //以上模块根据需要引入

                //分页器
            $.ajax({// 发送网络请求获取数据
                    url: baseURL + "/admin_info/list_page",
                    type: 'GET',
                    success: function (data) {
                        var totalPages = data.totalPages;
                        var totalElements = data.totalElements;
                        var pageSize = data.size;
                        var currentPage = data.number + 1;
                        
                        document.getElementById("span-count").innerHTML = '共有数据：' + totalElements + ' 条'

                        var pageConfig = {
                        cont: 'page', // 分页容器的 ID
                        pages: totalPages, // 总页数
                        limit: pageSize, // 每页显示的记录数
                        curr: currentPage, // 当前页码
                        jump: function (obj, first) {
                            if (!first) {
                                currentPage = obj.curr;
                                $.ajax({
                                    url:baseURL + "/admin_info/list_page?pageNo=" + currentPage,
                                    contentType:"application/json; charset=utf-8",
                                    success:function (data){
                                    showlist(data.content);
                                    }
                                })
                            }
                        }
                        };
                        
                        // 渲染分页器
                        laypage(pageConfig);
                        showlist(data.content);
                    },
                    error: function () {
                        layer.msg('网络请求发生错误');
                    }
                });
              
            });

            //批量删除提交
             function delAll () {
                layer.confirm('确认要删除吗？',function(index){
                    // 获取tbody
                    var tbody = document.getElementById("admin-list");
                    var rows = tbody.getElementsByTagName("tr");
                    for(let i = 1; i < rows.length; i++){
                        var adminId = rows[i].cells[1].textContent;  // 获取第二个单元格的文本内容
                        var checked = rows[i].cells[0].querySelector('input[type="checkbox"]').checked;  // 获取第二个单元格的文本内容
                        
                        if(checked){
                            $.ajax({
                                url:baseURL + "/admin_info/deleteAdminInfoByAdminId/" + adminId,
                                contentType:"application/json; charset=utf-8",
                                success:function (data){
                                    console.log(data);
                                }
                            });
                            console.log(i);
                        }
                    }
                    //捉到所有被选中的
                    location.reload();//刷新父页面
                    layer.msg('删除成功', {icon: 1});
                });
             }
             /*添加*/
            function admin_add(title,url,id,name,w,h){
                x_admin_show(title,url,id,name,w,h);
            }

             /*停用*/
            function admin_stop(obj,id){
                layer.confirm('确认要停用吗？',function(index){
                    //发异步把用户状态进行更改
                    $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="admin_start(this,id)" href="javascript:;" title="启用"><i class="layui-icon">&#xe62f;</i></a>');
                    $(obj).parents("tr").find(".td-status").html('<span class="layui-btn layui-btn-disabled layui-btn-mini">已停用</span>');
                    $(obj).remove();
                    layer.msg('已停用!',{icon: 5,time:1000});
                });
            }

            /*启用*/
            function admin_start(obj,id){
                layer.confirm('确认要启用吗？',function(index){
                    //发异步把用户状态进行更改
                    $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="admin_stop(this,id)" href="javascript:;" title="停用"><i class="layui-icon">&#xe601;</i></a>');
                    $(obj).parents("tr").find(".td-status").html('<span class="layui-btn layui-btn-normal layui-btn-mini">已启用</span>');
                    $(obj).remove();
                    layer.msg('已启用!',{icon: 6,time:1000});
                });
            }
            
            //修改密码
            function admin_edit (event,title,url,id,name,w,h) {
                var button = event.target;  // 获取当前点击的按钮元素
                var row = button.parentElement.parentElement.parentElement;  // 获取该行的元素
                var adminId = row.cells[1].textContent;  // 获取第二个单元格的文本内容
                var adminName = row.cells[2].textContent;
                
                layer.open({
                    type: 2,
                    area: [w+'px', h +'px'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade:0.4,
                    title: title,
                    content: url+
                    '?adminId=' + adminId + 
                    '&adminName=' + adminName
                });
            }
            /*删除*/
            function admin_del(event){
                var button = event.target;  // 获取当前点击的按钮元素
                var row = button.parentElement.parentElement.parentElement;  // 获取该行的元素
                var adminId = row.cells[1].textContent;  // 获取第二个单元格的文本内容
                
                layer.confirm('确认要删除吗？',function(index){
                    $.ajax({
						url:baseURL + "/admin_info/deleteAdminInfoByAdminId/" + adminId,
						contentType:"application/json; charset=utf-8",
						success:function (data){
                           console.log(data);
						}
					});
                    //发异步删除数据
                    row.remove();
                    location.reload();//刷新父页面
                    layer.msg('已删除!',{icon:1,time:1000});
                });
            }

            $('#form-query').submit(function(event){
                        // 阻止表单默认的提交行为
                        event.preventDefault();
                        const adminName = $("#adminName-query").val();

                        console.log(adminName);

                        var url = baseURL + "/admin_info/findAdminInfosByAdminNameLike?adminName=" + adminName; // 请求URL
                        
                        $.ajax({
                            url: url,
                            type: 'GET',
                            contentType:"application/json; charset=utf-8",
                            success:function(data){
                                var totalPages = data.totalPages;
                                var totalElements = data.totalElements;
                                var pageSize = data.size;
                                var currentPage = data.number + 1;
                                
                                document.getElementById("span-count").innerHTML = '共有数据：' + totalElements + ' 条'

                                var pageConfig = {
                                    cont: 'page', // 分页容器的 ID
                                    pages: totalPages, // 总页数
                                    limit: pageSize, // 每页显示的记录数
                                    curr: currentPage, // 当前页码
                                    jump: function (obj, first) {
                                        if (!first) {
                                            currentPage = obj.curr;
                                            $.ajax({
                                                url:url + "&pageNo=" + currentPage,
                                                contentType:"application/json; charset=utf-8",
                                                success:function (data){
                                                showlist(data.content);
                                                }
                                            })
                                        }
                                    }
                                };
                                
                                // 渲染分页器
                                laypage(pageConfig);
                                showlist(data.content);
                            },
                        });
                    });


            /*展示表格*/
            function showlist(data){
                //提前获取数据
                var checkinnerHTML = document.getElementById('td-check').innerHTML;
                var statusinnerHTML = document.getElementById('td-status').innerHTML;
                var manageinnerHTML = document.getElementById('td-manage').innerHTML;

                // 获取tbody
                var tbody = document.getElementById("admin-list");
                var rows = tbody.getElementsByTagName("tr");
                for(var i = 0; i < rows.length; i++){
                    rows[i].style.display = "none";
                }

                for(var i = 0; i < data.length; i++){
                    // 创建一个行元素
                    var row = document.createElement('tr');
                    // 创建td单元格 
                    var tdcheck = document.createElement('td'); tdcheck.id = "td-check"; tdcheck.className = "td-check"
                    var tdadminId = document.createElement('td');
                    var tdadminName = document.createElement('td');
                    var tdadminRole = document.createElement('td');
                    var tdstatus = document.createElement('td'); tdstatus.id = "td-status";tdstatus.className="td-status"
                    var tdmanage = document.createElement('td'); tdmanage.id = "td-manage";tdmanage.className="td-manage"

                    //显示数据
                    tdcheck.innerHTML = checkinnerHTML;
                    tdadminId.innerHTML = data[i].adminId;
                    tdadminName.innerHTML = data[i].adminName;
                    tdadminRole.innerHTML = data[i].adminRole;
                    tdstatus.innerHTML = statusinnerHTML;     
                    tdmanage.innerHTML = manageinnerHTML;            

                    // 将新建的td加入新建的tr行
                    
                    if(adminRole=="超级管理员"){
                        row.appendChild(tdcheck);
                        
                    }

                    row.appendChild(tdadminId);
                    row.appendChild(tdadminName);
                    row.appendChild(tdadminRole);
                    row.appendChild(tdstatus);
                    
                    if(adminRole=="超级管理员"){
                        row.appendChild(tdmanage);
                    }
                        
                    // 将这行加到tbody
                    tbody.appendChild(row);
                }
            }
            </script>
            <script>
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
          var s = document.getElementsByTagName("script")[0]; 
          s.parentNode.insertBefore(hm, s);
        })();
        </script>
    </body>
</html>